package com.jevons.muffin.adapter;

import android.util.SparseArray;
import android.view.View;

import com.jevons.muffin.R;


/**
 * ViewHolder 工具类（adapter数据又优化类）
 * create at:  2016/4/28
 */
public class ViewHolder {

    /**
     * ViewHolder优化，复用item
     *
     * @param convertView View
     * @param id          资源id
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T get(View convertView, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag(R.id.view_holder_tag);
        if (null == viewHolder) {
            viewHolder = new SparseArray<View>();
            convertView.setTag(R.id.view_holder_tag, viewHolder);
        }

        View view = viewHolder.get(id);
        if (null == view) {
            view = convertView.findViewById(id);
            viewHolder.put(id, view);
        }
        return (T) view;
    }
}
